/**
 *@description 用户画像》营销创意偏好》整体互动意愿
 *@author yhw
 */
import {
  marketingQuery
} from '../services/Marketing';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'Marketing',

  state: {
    marketingData: null,
    marketingLoading: false,
  },

  subscriptions: {},

  effects: { * marketingQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'marketingLoading'
      });

      const {
        data
      } = yield call(marketingQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'marketingQuerySuccess',
          payload: {
            marketingData: temp,
          },
        });
      } else {
        error("获取整体互动意愿失败");
      }
    },

  },

  reducers: {
    spreadLoading(state) {
      return {...state,
        spreadLoading: true
      };
    },
    spreadQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        spreadLoading: false
      };
    },
    //===================================
    marketingLoading(state) {
      return {...state,
        marketingLoading: true
      };
    },
    marketingQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        marketingLoading: false
      };
    },

  },
};